/*-------------------------------------------------------------------------
 *
 * logicalrelation.h
 *	  逻辑复制关系映射的关系定义。
 *
 * Portions Copyright (c) 2016-2022, PostgreSQL Global Development Group
 *
 * src/include/replication/logicalrelation.h
 *
 *-------------------------------------------------------------------------
 */
#ifndef LOGICALRELATION_H
#define LOGICALRELATION_H

#include "access/attmap.h"
#include "replication/logicalproto.h"

typedef struct LogicalRepRelMapEntry
{
	LogicalRepRelation remoterel;	/* 键是 remoterel.remoteid */

	/*
	 * 有效性标志 -- 当为假时，在下一个
	 * logicalrep_rel_open 时重新验证所有派生信息。 (当 localrel 打开时，我们假定我们对该关系的锁
	 * 确保信息保持有效。)
	 */
	bool		localrelvalid;

	/* 映射到本地关系。 */
	Oid			localreloid;	/* 本地关系 ID */
	Relation	localrel;		/* relcache 条目（关闭时为 NULL） */
	AttrMap    *attrmap;		/* 本地属性到远程属性的映射 */
	bool		updatable;		/* 是否可以应用更新/删除？ */

	/* 同步状态。 */
	char		state;
	XLogRecPtr	statelsn;
} LogicalRepRelMapEntry;

extern void logicalrep_relmap_update(LogicalRepRelation *remoterel);
extern void logicalrep_partmap_reset_relmap(LogicalRepRelation *remoterel);

extern LogicalRepRelMapEntry *logicalrep_rel_open(LogicalRepRelId remoteid,
												  LOCKMODE lockmode);
extern LogicalRepRelMapEntry *logicalrep_partition_open(LogicalRepRelMapEntry *root,
														Relation partrel, AttrMap *map);
extern void logicalrep_rel_close(LogicalRepRelMapEntry *rel,
								 LOCKMODE lockmode);
#ifdef FDDM


//返回true，继续走下面的流程，PG内核代码会报错，返回false，跳过
typedef bool (*table_missing_conflict_hook_type)(LogicalRepRelation *remoterel, LogicalRepTupleData *tup);

//处理原目标列缺失，返回true，继续走下面的流程，PG内核代码会报错，返回false，跳过
typedef bool (*column_missing_conflict_hook_type)(LogicalRepRelation *remoterel, 
			LogicalRepTupleData *tup, TupleDesc	local_desc, LogicalRepRelMapEntry *entry);

extern LogicalRepRelMapEntry *
fdd_logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMODE lockmode, LogicalRepTupleData *tup,  bool *skip, column_missing_conflict_hook_type column_missng_hook);
#endif
#endif							/* LOGICALRELATION_H */
